const BaseController = require('./base_controller.js');
const StoreManagementService = require('../service/store_management_service.js');

class StoreManagementController extends BaseController {

  /**
   * 添加门店
   */
  async addStore() {
    this.ensureAuthenticatedForWrite();

    // 数据校验
    let rules = {
      name: 'required|string|min:1|max:100|name=门店名称',
      address: 'required|string|min:1|max:200|name=门店地址',
      longitude: 'required|float|name=经度',
      latitude: 'required|float|name=纬度',
      phone: 'required|string|min:11|max:20|name=联系电话',
      openingHours: 'string|max:50|name=营业时间',
      coverImage: 'string|max:200|name=封面图片',
      services: 'array|name=支持服务',
    };

    let input = this.validateData(rules);

    const service = new StoreManagementService();
    return await service.addStore(input);
  }

  /**
   * 删除门店
   */
  async deleteStore() {
    this.ensureAuthenticatedForWrite();

    // 数据校验
    let rules = {
      storeId: 'required|string|name=门店ID',
    };

    let input = this.validateData(rules);

    const service = new StoreManagementService();
    await service.deleteStore(input.storeId);
    return { success: true, message: '门店删除成功' };
  }

  /**
   * 更新门店
   */
  async updateStore() {
    this.ensureAuthenticatedForWrite();

    // 数据校验
    let rules = {
      storeId: 'required|string|name=门店ID',
      name: 'string|min:1|max:100|name=门店名称',
      address: 'string|min:1|max:200|name=门店地址',
      longitude: 'float|name=经度',
      latitude: 'float|name=纬度',
      phone: 'string|min:11|max:20|name=联系电话',
      openingHours: 'string|max:50|name=营业时间',
      coverImage: 'string|max:200|name=封面图片',
      services: 'array|name=支持服务',
      status: 'int|in:0,1|name=门店状态',
    };

    let input = this.validateData(rules);

    const service = new StoreManagementService();
    return await service.updateStore(input.storeId, input);
  }

  /**
   * 获取门店列表
   */
  async getStoreList() {
    // 数据校验 - 允许所有已认证用户查看门店列表
    let rules = {
      page: 'int|min:1|default=1|name=页码',
      size: 'int|min:1|max:50|default=20|name=每页数量',
      status: 'int|name=门店状态',
      search: 'string|max:50|name=搜索关键词',
    };

    let input = this.validateData(rules);

    const service = new StoreManagementService();
    const result = await service.getStoreList(input);
    return result;
  }
}

module.exports = StoreManagementController;
